<template>
  <div>
    <el-form ref="deptForm" :rules="rules" label-width="120px" :model="form">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, updateDepartments } from '@/api/departments'

export default {
  name: 'VueAdminTemplateMasterDeptdialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    pid: {
      type: String,
      default: ''
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      employees: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度为 1-10', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 10, message: '长度为 1-10', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门管理者', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 10, message: '长度为 1-10', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    isEdit: {
      handler(newVal) {
        if (newVal) {
        //   console.log(this.row)
        // 展开运算符
        //   this.form = { ...this.row }
        // JSON.parse
          this.form = JSON.parse(JSON.stringify(this.row))
        //   console.log(this.form)
        }
      },
      immediate: true
    }
  },

  mounted() {

  },
  created() {
    this.loadEmployee()
  },

  methods: {
    hSubmit() {
      this.$refs.deptForm.validate(async(valid) => {
        if (!valid) return this.$message.error('请正确填写')
        this.form.pid = this.pid
        // console.log(this.pid)
        // console.log(this.form.pid)
        let res = {}
        if (this.isEdit) {
          res = await updateDepartments(this.form)
        } else {
          res = await addDepartments(this.form)
        }
        // console.log(res)
        if (res.code === 10000) {
          this.$emit('update:visible', false)
          this.$refs.deptForm.resetFields()
          this.$emit('success')
        }
      })
    },
    hCancel() {
    //   console.log(111)
      //   console.dir(this.$emit)
      this.$emit('update:visible', false)
      this.$refs.deptForm.resetFields()
    },
    async loadEmployee() {
      const res = await getEmployeeSimple()
      //   console.log(res)
      if (res.code === 10000) {
        this.employees = res.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
